<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
	session_start();
	$dbconn = pg_connect("host=localhost port=5432 dbname=CinemaEnri user=postgres password=Quie1hoh");
	if (!$dbconn){
			die("Errore di connessione al database" . pg_last_error());
	}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="Css/screen.css" rel="stylesheet" type="text/css" media="screen"/>
<script language="JavaScript" type="text/JavaScript" src="Function.js" ></script>
<title>Cinema Enri - Modifica Film</title>
</head>

<body>
<div id="logo"><img src="Images/Logo.jpg" width="220" height="100"/></div>
        <div id="Header">
             <div id="cerca">
            </div>
        </div>
        <div id="main">
            <div id="menu">
                <ul>
                	<div id="infoCinema">
                    	<div class="titolo">Info Cinema</div>
                        <li><a href="Index.php">Home</a></li>
                        <li><a href="Programmazione.php">Programmazione</a></li>
                    </div>
                     <?php
						$user=$_SESSION['User'];
						$admin=$_SESSION['Admin'];
						if (isset($user)){
					?>
                    <div id="utente">
                    	<div class="titolo">Servizi Utente</div>
                        <li><a href="Prenotazioni.php">Prenota un film</a></li>
                        <li><a href="PaginaPersonale.php">Pagina Personale</a></li>
                    </div>
                    <?php
						}
						if ($admin=="t"){
					?>
                    <div id="admin">
                    	<div class="titolo">Amministratore</div>
                        <li><a href="InsSpettacoli.php">Inserimento Spettacoli</a></li>
                        <li><a href="ModSpettacoli.php">Modifica Spettacoli</a></li>
                        <li><a href="InsFilm.php">Inserimento Film</a></li>
                        <li><a href="ModFilm.php">Modifica Film</a></li>
                        <li><a href="InsAttori.php">Inserimento Attori</a></li>
                        <li><a href="ModAttori.php">Modifica Attori</a></li>
                        <li><a href="InsProduzioni.php">Inserimento Produzioni</a></li>
                        <li><a href="ModPrenotazioni.php">Modifica Prenotazioni</a></li>
                    </div>
					<?php
						}
					?>
                <ul>
            </div>
            <div id="path">
            </div>
            <div id="content">
            	<h1>Modifica informazioni film</h1>
                
                <?php
				
					$admin=$_SESSION['Admin'];
						if ($admin=="t"){
							$id=$_POST['id'];
					
							if (!isset($id)){
								$qry = 'SELECT * FROM "tblFilm"';
								$result = pg_query($dbconn, $qry);
					
								while($riga = pg_fetch_assoc($result)) {
									echo "<form action=ModFilm.php method=post >
										<b>".$riga['Titolo']."</b>";
									echo "<input type=hidden name=id value=".$riga['ID']." /> &nbsp; &nbsp;";	
									echo "<input type=submit name=button value=Modifica /></form>";
								}
							}
							else{
								$qry = "SELECT * FROM \"tblFilm\" WHERE \"ID\"=".$id."";
								$result = pg_query($dbconn, $qry);
					
								while($riga = pg_fetch_assoc($result)) {

									$qry2 = "SELECT * FROM \"tblAttoriFilm\" af JOIN \"tblCast\" c ON af.\"Attore\" = c.\"ID\" WHERE 
											\"Film\" = " . $id;
									$result2 = pg_query($dbconn, $qry2);									
																
									echo "<div id='castFilm'><form action=ModAttoriFilm.php method=post><h2>Attori del film: </h2>";
									echo '<textarea name="castFilm" readonly="readonly" cols=30 rows=10>';
									while($riga2 = pg_fetch_assoc($result2))
										echo $riga2['Nome'].' '.$riga2['Cognome'] . ' &#13;&#10';
									echo "</textarea><br /><input type=submit value='Modifica Cast' />";
									echo "<input type=hidden name=id value=".$id." /></form></div>";
									



									echo "<form action=ModFilmOK.php method=post>";
									echo "<input type=hidden name=IDFilm value=".$id." /> &nbsp; &nbsp;";
              						echo '<h2>Titolo: </h2><input type=text name=titolo value="'.$riga['Titolo'].'" class=textfield />';
			  
			 						$qry = 'SELECT * FROM "tblCast"';
			  						$result2 = pg_query($dbconn, $qry);
									echo "<h2>Regista: </h2><select name=regista class=textfield>";
									echo "<option value=0 >------</option>";
									while($rigaCast = pg_fetch_assoc($result2)) {
										$reg=$rigaCast['Nome']. " " . $rigaCast['Cognome'];
										if ($riga['Regista']==$rigaCast['ID']){
											echo "<option value=".$rigaCast['ID']." selected=selected>".$reg."</option>";
										}else{
											echo "<option value=".$rigaCast['ID'].">".$reg."</option>";
									}
								}
								echo "</select>";
						
								$qry = 'SELECT * FROM "tblProduzioni"';
								$result2 = pg_query($dbconn, $qry);
								echo "<h2>Produzione: </h2><select name=produzione class=textfield>";
								echo "<option value= >------</option>";
								while($rigaProd = pg_fetch_assoc($result2)) {
							
									if ($riga['Produzione']==$rigaProd['ID']){
										echo "<option value=".$rigaProd['ID']." selected=selected>".$rigaProd['Nome']."</option>";
									}else{
										echo "<option value=".$rigaProd['ID'].">".$rigaProd['Nome']."</option>";
									}
								}
								echo "</select>";
						
								echo "<h2>Durata: </h2><input type=text name=durata value=".$riga['Durata']." class=textfield />";
							
								$qry = 'SELECT * FROM "tblGeneri"';
								$result2 = pg_query($dbconn, $qry);
								echo "Genere: <select name=genere class=textfield>";
								while($rigaGenere = pg_fetch_assoc($result2)) {
									if ($riga['Genere']==$rigaGenere['Nome']){
										echo "<option value=".$rigaGenere['Nome']." selected=selected>".$rigaGenere['Nome']."</option>";
									}else{
										echo "<option value=".$rigaGenere['Nome'].">".$rigaGenere['Nome']."</option>";
									}
								}
								echo "</select>";
						
								$anno=1930;
								$date = getdate();
								echo "<h2>Anno: </h2><select name=anno class=textfield>";
								while ($anno<=$date['year']){
									if ($riga['Anno']==$anno){
										echo "<option value=".$anno." selected=selected>".$anno."</option>";
									}else{
										echo "<option value=".$anno.">".$anno."</option>";
									}
									$anno=$anno+1;
								}
								echo "</select>";
						
								echo "<h2>Nazionalità: </h2><input type=text name=nazione value='".$riga['Nazionalita']."' class=textfield />
                  					  <h2>Descrizione: </h2><textarea name=descrizione class=textArea>".$riga['Descrizione']."</textarea>
                  			  	  	  <h2>Sito ufficiale: </h2><input type=text name=sito value='".$riga['SitoUfficiale']."' class=textfield />
                    		  	  	  <h2>Trailer: </h2><input type=text name=trailer value='".$riga['Trailer']."' class=textfield />";
							  
							 	if ($handle = opendir('Images/Film')) { 
  									echo "<h2>Immagine: </h2><select name=immagine class=textfield onchange=Imgpreview(this);> <option value=>------</option>";
  					 				while (false !== ($file = readdir($handle))) {  
      				 					if ( $file == ".." || $file == "." || $file == ".svn") continue;
										if ($file==$riga['Immagine']){
											echo "<option value=".$file." selected=selected>".$file."</option>";
										}else{
											echo "<option value=".$file.">".$file."</option>";
										}
   									} 
									echo "</select>";
									echo "<div id=preview></div>";
  									closedir($handle);  
								} 		
                   			 
								if ($riga['3D']=="t"){
									 echo "<h2>3D: </h2><input type=checkbox name=3d checked=checked class=textfield />";
								}else{
									echo "<h2>3D: </h2><input type=checkbox name=3d class=textfield />";
								}
                   				echo "<input type=submit name=button value=Modifica />";
                				echo "<input type=submit name=button value=Elimina /></form>";
								echo "<h1><a href=ModFilm.php >Indietro</a></h1>";
							}
						}
				}else{
					echo "Accesso vietato alla pagina"; 
				 
				}
				?>
                
            </div>
            
            <div id="menuDestro">
                <div id="login" >
                	<?php
						$user=$_SESSION['User'];
					 	if (isset($user)){
								echo "<h2>Stato</h2>";
								echo "<h3>Benvenuto ".$user."</h3>";
								echo "<a href=LogOut.php>LogOut</a>";
						}else{
					?>
                	<h2>Login</h2>
                	<form action="Index.php" method="post">
                    	Username:<input type="text" name="Username" />
                        Password:<input type="password" name="Password" />
                        <input type="submit" value="Login" />
                         Se non sei registrato: <a href="Registrazione.php" >Registrati</a>
                    </form>
                   <?php
						}
				   ?>
                </div>
            </div>
       </div>
</div>



</body>
</html>